package com.caishi.lkx.user.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.context.RequireContext;
import com.caishi.lkx.common.role.Role;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.user.ienum.type.UserType;
import com.caishi.lkx.user.model.AgentRechargeModel;
import com.caishi.lkx.user.model.AgentUserModel;
import com.caishi.lkx.user.model.UserModel;
import com.caishi.lkx.user.service.IAgentRechargeService;
import com.caishi.lkx.user.service.IAgentUserService;
import com.caishi.lkx.user.service.IUserService;
import com.zzw.common.Wrappers;
import com.zzw.common.mysql.KerayLambdaQueryWrapper;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "代理商等级接口")
@RestController
@RequestMapping("/user/cu/agent/recharge")
@Roles({Role.onlyLogin})
public class AgentRechargeApi extends AdminBaseApi<IAgentRechargeService, AgentRechargeModel, String> {

    @Resource
    private IAgentRechargeService agentRechargeService;
    @Resource
    private IUserService<UserModel> userService;
    @Resource
    private IAgentUserService agentUserService;

    @Override
    public IAgentRechargeService loadService() {
        return agentRechargeService;
    }

    @Override
    protected LambdaQueryWrapper<AgentRechargeModel> baseWrapper(AgentRechargeModel model, JSONObject otherParam) {
        AgentRechargeModel model1 = model.clearEmptyStringField();
        KerayLambdaQueryWrapper<AgentRechargeModel> lambdaQuery = Wrappers.lambdaQuery(model);
        if (model.getUserId() != null) {
            Integer userType = userService.getById(model.getUserId()).getUserType();
            if (UserType.intAnyHave(userType,new UserType[]{UserType.agent})) {
                List<String> list = agentUserService.selectList(Wrappers.<AgentUserModel>lambdaQuery()
                        .eq(AgentUserModel::getParentId, model.getUserId()).select(AgentUserModel::getId)).parallelStream().map(AgentUserModel::getId).toList();
                list=new ArrayList<>(list);
                list.add(model.getUserId());
                model.clearEmptyStringField("userId");
                lambdaQuery.in(AgentRechargeModel::getUserId,list);
            }
        }else{
            if (context.getGroupId()!= null && context.getGroupId() != 0L) {
                List<String> list = agentUserService.selectList(Wrappers.<AgentUserModel>lambdaQuery()
                        .eq(AgentUserModel::getBindGroupId, context.getGroupId()).select(AgentUserModel::getId)).parallelStream().map(AgentUserModel::getId).toList();
                list=new ArrayList<>(list);
                model.clearEmptyStringField("userId");
                lambdaQuery.in(AgentRechargeModel::getUserId,list);
            }
        }
        model1.setAmount(null);
        return lambdaQuery;
    }

    @Override
    protected List<AgentRechargeModel> listCover(List<AgentRechargeModel> result, JSONObject otherParam) {
        return result.parallelStream().peek(r -> {
            UserModel user = userService.getById(r.getCreatedBy());
            if (null != user) {
                r.setCreatedByName(user.getName());
            }
            r.setRechargeUser(userService.selectName(r.getUserId()));
        }).collect(Collectors.toList());
    }

    @Override
    @RequireContext(value = com.caishi.lkx.common.context.Role.CHANNEL)
    public IPage<AgentRechargeModel> page(@ModelAttribute Page<AgentRechargeModel> page, @ModelAttribute AgentRechargeModel model, @NotNull JSONObject otherParam) {
        Boolean all = otherParam.getBool("all", false);
        otherParam.set("groupId",context.getGroupId());
        if (!all) {
            model.setUserId(userContext.currentUserId());
            AgentUserModel byId = agentUserService.getById(userContext.currentUserId());
            if (null == byId) {
                return page;
            }
            if (StrUtil.isNotBlank(byId.getParentId())) {
                model.setUserId(byId.getParentId());
            }

        }
        return super.page(page, model, otherParam);
    }

}
